
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<div class="modal" id="addLineDiv" tabindex="-1" role="dialog" aria-hidden="true">

	<div class="modal-dialog" role="document">

		<div class="modal-content animated bounceInRight">

			<div class="modal-header">
				<h4 class="modal-title redragon-modal-title">计划行编辑</h4>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			</div>

			<div class="modal-body" style="padding-bottom: 20px;">
				<%-- 导入提示信息框 --%>
			    <c:if test="${hint!=null&&hint!=''}">
			   		<jsp:include page="../../common/alert/alert.jsp">
			   			<jsp:param value="${hint}" name="alertType"/>
			   			<jsp:param value="${alertMessage}" name="alertMessage"/>
			   		</jsp:include>
			    </c:if>
			
				<form id="lineForm">
					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>物料选择</label>
						<div class="col-sm-9">
							<select class="chosen-select form-control" name="materialCode" id="materialCode">
	                        	<option value="" selected="selected">请选择...</option>
	                        	<c:forEach items="${requestScope.materialMap}" var="material">
	                        		<option value="${material.key}">${material.value}（${material.key}）</option>
	                        	</c:forEach>
	                        </select>
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>物料属性</label>
						<div class="col-sm-9">
							<select class="form-control" name="bomProperty" id="bomProperty">
	                        	<option value="" selected="selected">请选择...</option>
	                        	<c:forEach items="${requestScope.bomPropertyMap}" var="bomProperty">
	                        		<option value="${bomProperty.key}">${bomProperty.value}</option>
	                        	</c:forEach>
	                        </select>
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label">单位</label>
						<div class="col-sm-9">
							<input id="materialUnit" type="text" class="form-control" value="${requestScope.prodMpsLine.materialUnit}" readonly="readonly">
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label">规格</label>
						<div class="col-sm-9">
							<input id="materialStandard" type="text" class="form-control" value="${requestScope.prodMpsLine.materialStandard}" readonly="readonly">
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>计划数量</label>
						<div class="col-sm-9">
							<input id="planQuantity" name="planQuantity" type="text" class="form-control" value="${requestScope.prodMpsLine.planQuantity}">
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>计划开始日期</label>
						<div class="col-sm-9">
							<div class="input-group date">
								<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
								<input id="planStartDate" name="planStartDate" type="text" class="form-control" value="<fmt:formatDate value="${requestScope.prodMpsLine.planStartDate}" pattern="yyyy-MM-dd"/>" autocomplete="off">
							</div>
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label"><span class="text-danger">*</span>计划结束日期</label>
						<div class="col-sm-9">
							<div class="input-group date">
								<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
								<input id="planEndDate" name="planEndDate" type="text" class="form-control" value="<fmt:formatDate value="${requestScope.prodMpsLine.planEndDate}" pattern="yyyy-MM-dd"/>" autocomplete="off">
							</div>
						</div>
					</div>
					<div class="hr-line-dashed"></div>
					
					<div class="form-group row">
						<label class="col-sm-3 col-form-label">备注</label>
						<div class="col-sm-9">
							<input id="memo" name="memo" type="text" class="form-control" value="${requestScope.prodMpsLine.memo}">
						</div>
					</div>
					<div class="hr-line-dashed"></div>

					<div class="form-group row m-b-none">
						<div class="col-sm-12 col-sm-offset-2 text-right">
							<button class="btn btn-white btn-lg" type="button"
								data-dismiss="modal">返回</button>
							&nbsp;
							<button
								class="ladda-button ladda-button-demo btn btn-primary btn-lg"
								data-style="expand-right">
								&nbsp;&nbsp;确定&nbsp;&nbsp;<i class="fa fa-check-square-o"></i>
							</button>
						</div>
					</div>
					
					<input type="hidden" id="mpsHeadCode" name="mpsHeadCode" value="${param.mpsHeadCode}"> 
					<input type="hidden" id="mpsLineId" name="mpsLineId" value="${requestScope.prodMpsLine.mpsLineId}"> 
					<input type="hidden" id="mpsLineCode" name="mpsLineCode" value="${requestScope.prodMpsLine.mpsLineCode}"> 
					<input type="hidden" id="createdDate" name="createdDate" value="${requestScope.prodMpsLine.createdDate}"> 
					<input type="hidden" id="createdBy" name="createdBy" value="${requestScope.prodMpsLine.createdBy}">
				</form>
			</div>

		</div>

	</div>

</div>

<!-- Chosen -->
<script src="js/plugins/chosen/chosen.jquery.js"></script>

<script>
	$(document).ready(function() {
		//初始化materialCode
		if("${requestScope.prodMpsLine.materialCode}"!=""){
			$("#materialCode").val("${requestScope.prodMpsLine.materialCode}");
		}
		
		//初始化bomProperty
		if("${requestScope.prodMpsLine.bomProperty}"!=""){
			$("#bomProperty").val("${requestScope.prodMpsLine.bomProperty}");
		}
		
		//初始化chosen-select
		$('.chosen-select').chosen({width: "100%"});
		
		//设置日期插件
		$('#planStartDate').datepicker({
			todayBtn : "linked",
			keyboardNavigation : true,
			forceParse : true,
			calendarWeeks : false,
			autoclose : true,
			format: 'yyyy-mm-dd',
			language: 'zh-CN',
		});
		
		//设置日期插件
		$('#planEndDate').datepicker({
			todayBtn : "linked",
			keyboardNavigation : true,
			forceParse : true,
			calendarWeeks : false,
			autoclose : true,
			format: 'yyyy-mm-dd',
			language: 'zh-CN',
		});
		
		//切换物料选择获取物料信息
		$("#materialCode").change(function(){
			getBomMaterialInfoAjax($("#materialCode").val());
		});
		
		//表单提交
		var l = $('.ladda-button-demo').ladda();

		l.click(function() {
			$("#lineForm").valid();
			//l.ladda('stop');
		});

		$("#lineForm").validate({
			rules : {
				materialCode : {
					required : true,
				},
				bomProperty : {
					required : true,
				},
				planQuantity : {
					required : true,
					gtZero : true,
				},
				planStartDate : {
					required : true,
				},
				planEndDate : {
					required : true,
				},
			},
			submitHandler: function(form) {
				var submitFlag = "Y";
				
				//验证计划行数量
				/*
				var quantity = parseFloat($("#quantity").val());
				var inputedQuantity = parseFloat($("#inputedQuantity").val());
				var inputQuantity = parseFloat($("#inputQuantity").val());
				if($("#inputType").val()=="PO_RETURN"){
					if(inputQuantity>inputedQuantity){
						submitFlag = "N";
						redragonJS.alert("退货数量("+inputQuantity+")不能大于已入库数量("+inputedQuantity+")");
						return false;
					}
				}else{
					if(inputQuantity>redragonJS.numberSub(quantity, inputedQuantity)){
						submitFlag = "N";
						redragonJS.alert("入库数量("+inputQuantity+")不能大于采购订单行数量("+quantity+")-已入库数量("+inputedQuantity+")");
						return false;
					}
				}*/
			
				//表单提交
				if(submitFlag=="Y"){
					l.ladda('start');
					editLine();
				}
		    }
		});
		
	});
	
	//异步编辑付款行
	function editLine(){
		redragonJS.loading("ibox-content1");
	
		$.ajax({
			type: "post",
			url: "web/prodMpsLine/editProdMpsLine",
			data: {"materialCode": $("#materialCode").val(), "bomProperty": $("#bomProperty").val(), "planQuantity": $("#planQuantity").val(), "planStartDate": $("#planStartDate").val(), "planEndDate": $("#planEndDate").val(),
				   "memo": $("#memo").val(), "mpsHeadCode": $("#mpsHeadCode").val(), "mpsLineId": $("#mpsLineId").val(), "mpsLineCode": $("#mpsLineCode").val(),
				   "createdDate": $("#createdDate").val(), "createdBy": $("#createdBy").val()},
			async: false,
			dataType: "html",
			cache: false,
			success: function(data){
				redragonJS.removeLoading("ibox-content1");
				$('#addLineDiv').modal('hide');
				$('.ladda-button-demo').ladda('stop');
				
				var json = JSON.parse(data);
				if(json.result=="success"){
					getLineTab($("#mpsHeadCode").val());
				}else{
					redragonJS.alert("编辑行信息错误");
				}
				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
	
	//获取物料信息
	function getBomMaterialInfoAjax(materialCode){
		$.ajax({
			type: "post",
			url: "web/prodMpsLine/getBomMaterialInfoAjax",
			data: {"materialCode": materialCode},
			async: false,
			dataType: "json",
			cache: false,
			success: function(data){
				if(data!=""){
					$("#materialUnit").val(data.materialUnit);
					$("#materialStandard").val(data.materialStandard);
					$("#bomProperty").val(data.bomProperty);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				redragonJS.alert(textStatus);
			}
		});
	}
	
</script>
